﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;

public partial class _AlterarEmpresa : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["cnpj"] == null)
            Response.Redirect("default.aspx");
        else
        {
            if (!Page.IsPostBack)
            {
                SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
                SqlCommand consultaEmpresa = null;
                SqlDataReader drConsultaEmpresa = null;
                try
                {
                    conexaoIntranet.Open();
                    string instrucao = "SELECT * FROM CamaraEmpresas WHERE CNPJ = '" + Session["cnpj"].ToString() + "'";
                    consultaEmpresa = new SqlCommand(instrucao, conexaoIntranet);
                    drConsultaEmpresa = consultaEmpresa.ExecuteReader();
                    if (drConsultaEmpresa.HasRows)
                    {
                        drConsultaEmpresa.Read();
                        txtCNPJ.Text = drConsultaEmpresa["CNPJ"].ToString();
                        txtRazao.Text = drConsultaEmpresa["RazaoSocial"].ToString();
                        txtNomeFantasia.Text = drConsultaEmpresa["NomeFantasia"].ToString();
                        txtSegmentoAtuacao.Text = drConsultaEmpresa["SegmentoAtuacao"].ToString();
                        txtEndereco.Text = drConsultaEmpresa["Endereco"].ToString();
                        txtNumero.Text = drConsultaEmpresa["Numero"].ToString();
                        txtComplemento.Text = drConsultaEmpresa["Complemento"].ToString();
                        txtBairro.Text = drConsultaEmpresa["Bairro"].ToString();
                        txtCidade.Text = drConsultaEmpresa["Cidade"].ToString();
                        txtUF.Text = drConsultaEmpresa["UF"].ToString();
                        txtCEP.Text = drConsultaEmpresa["CEP"].ToString();
                        txtFonePre.Text = drConsultaEmpresa["DDDFone"].ToString();
                        txtFone.Text = drConsultaEmpresa["Fone"].ToString();
                        txtEmail.Text = drConsultaEmpresa["Email"].ToString();
                        txtSite.Text = drConsultaEmpresa["Site"].ToString();
                        txtResponsavelCadastro.Text = drConsultaEmpresa["ResponsavelCadastro"].ToString();
                        txtSenha.Text = drConsultaEmpresa["Senha"].ToString();
                        txtConfirma.Text = drConsultaEmpresa["Senha"].ToString();
                        drConsultaEmpresa.Close();
                    }
                }
                finally
                {
                    conexaoIntranet.Close();
                }
            }
        }
    }
    protected void btnCadastro_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand alteraEmpresa = null;
        try
        {
            conexaoIntranet.Open();
            string instrucao = "UPDATE CamaraEmpresas SET RazaoSocial = '"+ txtRazao.Text +"', NomeFantasia = '"+ txtNomeFantasia.Text +"', SegmentoAtuacao = '"+ txtSegmentoAtuacao.Text +"', Endereco = '"+ txtEndereco.Text +"', Numero = '"+ txtNumero.Text +"', Complemento = '"+ txtComplemento.Text +"', Bairro = '"+ txtBairro.Text +"', Cidade = '"+ txtCidade.Text +"', UF = '"+ txtUF.Text +"', CEP = '"+ txtCEP.Text +"', DDDFone = '"+ txtFonePre.Text +"', Fone = '"+ txtFone.Text +"', Email = '"+ txtEmail.Text +"', Site = '"+ txtSite.Text +"', ResponsavelCadastro = '"+ txtResponsavelCadastro.Text +"', Senha = '"+ txtSenha.Text +"' WHERE CNPJ = '"+ txtCNPJ.Text +"'";
            alteraEmpresa = new SqlCommand(instrucao, conexaoIntranet);
            alteraEmpresa.ExecuteNonQuery();
            Response.Redirect("mensagensc.aspx?id=4");
        }
        finally
        {
            conexaoIntranet.Close();
        }
    }
}